package name.sra.util;

import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.List;

import javax.imageio.ImageIO;

import junit.framework.TestCase;

public class TestContiguousLine extends TestCase {
	
	private static double MEDIUM_LEVEL = 0.5;
	private BufferedImage inputImage;
	private BufferedImage outputImage;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		InputStream png0 = ClassLoader.getSystemResourceAsStream("png0.png");
		inputImage = ImageIO.read(png0);
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	public void testNoClearLine() {
		ImageLine line = new ImageLine(inputImage, outputImage, 31);
		line.calculateContiguousLines(MEDIUM_LEVEL);
		List<ImageLine> contiguousLines = line.getContiguousLines();
		
		assertEquals(0, contiguousLines.size());
	}
	
	public void testWholeClearLine() {
		
		ImageLine line = new ImageLine(inputImage, outputImage, 0);
		line.calculateContiguousLines(MEDIUM_LEVEL);
		List<ImageLine> contiguousLines = line.getContiguousLines();
		ImageLine firstLine = contiguousLines.get(0);
		
		assertEquals(1, contiguousLines.size());
		assertEquals(0, firstLine.getLineBegin());
		assertEquals(inputImage.getWidth() - 1, firstLine.getLineEnd());
		assertEquals(inputImage.getWidth(), firstLine.getLength());
	}
	
	public void testClearLineStartingFromBeginning()  {
		
		ImageLine line = new ImageLine(inputImage, outputImage, 7);
		line.calculateContiguousLines(MEDIUM_LEVEL);
		List<ImageLine> contiguousLines = line.getContiguousLines();
		
		assertEquals(1, contiguousLines.size());
		ImageLine firstLine = contiguousLines.get(0);
		assertEquals(0, firstLine.getLineBegin());
		assertEquals(1, firstLine.getLineEnd());
		assertEquals(2, firstLine.getLength());
	}

	public void testClearLineStartingFromMiddleEndingAtEnd()  {
		
		ImageLine line = new ImageLine(inputImage, outputImage, 28);
		line.calculateContiguousLines(MEDIUM_LEVEL);
		List<ImageLine> contiguousLines = line.getContiguousLines();
		
		assertEquals(1, contiguousLines.size());
		ImageLine firstLine = contiguousLines.get(0);
		assertEquals(3, firstLine.getLineBegin());
		assertEquals(inputImage.getWidth() - 1, firstLine.getLineEnd());
		assertEquals(2, firstLine.getLength());
	}
	
	public void testClearLineInTheMiddle()  {
		
		ImageLine line = new ImageLine(inputImage, outputImage, 17);
		line.calculateContiguousLines(MEDIUM_LEVEL);
		List<ImageLine> contiguousLines = line.getContiguousLines();
		
		assertEquals(1, contiguousLines.size());
		ImageLine firstLine = contiguousLines.get(0);
		assertEquals(1, firstLine.getLineBegin());
		assertEquals(3, firstLine.getLineEnd());
		assertEquals(3, firstLine.getLength());
	}
	
	public void testClearLinesAtBothEnds()  {
		
		ImageLine line = new ImageLine(inputImage, outputImage, 4);
		line.calculateContiguousLines(MEDIUM_LEVEL);
		List<ImageLine> contiguousLines = line.getContiguousLines();
		
		assertEquals(2, contiguousLines.size());
		ImageLine firstLine = contiguousLines.get(0);
		assertEquals(0, firstLine.getLineBegin());
		assertEquals(1, firstLine.getLineEnd());
		assertEquals(2, firstLine.getLength());
		ImageLine secondLine = contiguousLines.get(1);
		assertEquals(3, secondLine.getLineBegin());
		assertEquals(4, secondLine.getLineEnd());
		assertEquals(2, secondLine.getLength());
	}
	
	public void testOneClearDotAtBeginning()  {
		
		ImageLine line = new ImageLine(inputImage, outputImage, 15);
		line.calculateContiguousLines(MEDIUM_LEVEL);
		List<ImageLine> contiguousLines = line.getContiguousLines();
		
		assertEquals(1, contiguousLines.size());
		ImageLine firstLine = contiguousLines.get(0);
		assertEquals(0, firstLine.getLineBegin());
		assertEquals(0, firstLine.getLineEnd());
		assertEquals(1, firstLine.getLength());
	}

	public void testOneClearDotAtEnd()  {
		
		ImageLine line = new ImageLine(inputImage, outputImage, 30);
		line.calculateContiguousLines(MEDIUM_LEVEL);
		List<ImageLine> contiguousLines = line.getContiguousLines();
		
		assertEquals(1, contiguousLines.size());
		ImageLine firstLine = contiguousLines.get(0);
		assertEquals(inputImage.getWidth() - 1, firstLine.getLineBegin());
		assertEquals(inputImage.getWidth() - 1, firstLine.getLineEnd());
		assertEquals(1, firstLine.getLength());
	}


	public void testTwoClearDotsInTheMiddle()  {
		
		ImageLine line = new ImageLine(inputImage, outputImage, 21);
		line.calculateContiguousLines(MEDIUM_LEVEL);
		List<ImageLine> contiguousLines = line.getContiguousLines();
		
		assertEquals(2, contiguousLines.size());
		ImageLine firstLine = contiguousLines.get(0);
		assertEquals(1, firstLine.getLineBegin());
		assertEquals(1, firstLine.getLineEnd());
		assertEquals(1, firstLine.getLength());
		ImageLine secondLine = contiguousLines.get(1);
		assertEquals(3, secondLine.getLineBegin());
		assertEquals(3, secondLine.getLineEnd());
		assertEquals(1, secondLine.getLength());
	}
}
